#!/usr/bin/env python
# -*- coding: utf-8 -*-
from typing import TypeVar

from sqlalchemy.orm import Session

from app.common.dateutils import DateUtils
from app.core.constant_code import ConstantItem
from app.models import User
from pydantic import BaseModel

from app.dao.base_dao import BaseDao
from app.db.base_class import Base
from app.models.constant import Constant
from app.models.trade_calendar import TradeCalendar
from app.models.user_setting import UserSetting
from app.schemas.trade_calendar import TradeCalendarQuery


class TradeCalendarDao(BaseDao[TradeCalendar]):
    def __init__(self):
        super(TradeCalendarDao, self).__init__()
        self.modelClass = TradeCalendar
        self.id = TradeCalendar.id

    def selectLast(self, db:Session, queryWrapper: TradeCalendarQuery) -> TradeCalendar:
        dbData = db.query(TradeCalendar).filter(TradeCalendar.state == ConstantItem.STATE_ENABLED,
                                                TradeCalendar.calDate < DateUtils.today(),
                                                TradeCalendar.marketType == queryWrapper.marketType
                                                ).order_by(TradeCalendar.calDate.desc()).first()
        return dbData

tradeCalendarDao = TradeCalendarDao()
